package com.irm.controller;

import com.gadgets.framework.common.dto.HttpBaseResp;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.irm.dto.DraftOperDTO;
import com.irm.dto.MockInfoQueryDTO;
import com.irm.dto.MsgLogQueryDTO;
import com.irm.pojo.IrmMockInfo;
import com.irm.pojo.IrmMsgLog;
import com.irm.service.IDraftOperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2022-01-20
 */
@RestController
@RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "draft")
public class DraftController {

    @Autowired
    @Qualifier(value = "draftOperServiceImpl")
    private IDraftOperService draftOperService;

    /**
     * 报文操作方法
     * @param request                       网络请求
     * @param response                      网络应答
     * @param draftOperDTO                  报文申请信息
     * @return                              操作结果
     * @throws GadgetsServiceException      异常信息
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/oper.do")
    public HttpBaseResp operationDraftOper(HttpServletRequest request, HttpServletResponse response, DraftOperDTO draftOperDTO) throws GadgetsServiceException{
        draftOperService.operationDraftMock(draftOperDTO);
        return new HttpBaseResp();
    }

    /**
     * 查询 MockInfo 表信息
     * @param request                       网络请求
     * @param response                      网络应答
     * @param mockInfoQueryDTO              报文申请信息
     * @param pagination                    分页信息
     * @return                              应答结果
     * @throws GadgetsServiceException      异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/queryMockInfo.do")
    public HttpBaseResp<IrmMockInfo> queryMockInfo(HttpServletRequest request, HttpServletResponse response, MockInfoQueryDTO mockInfoQueryDTO, Pagination pagination) throws GadgetsServiceException{
        return new HttpBaseResp<>(draftOperService.selectMockInfo(mockInfoQueryDTO, pagination), pagination);
    }

    /**
     * 报文信息查询
     * @param request                       网络请求
     * @param response                      网络应答
     * @param queryDTO                      查询条件
     * @param pagination                    分页信息
     * @return                              应答结果
     * @throws GadgetsServiceException      服务异常
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/queryMsgLog.do")
    public HttpBaseResp<IrmMsgLog> queryMsgLog(HttpServletRequest request, HttpServletResponse response, MsgLogQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException{
        return new HttpBaseResp<>(draftOperService.selectMsgLog(queryDTO, pagination), pagination);
    }

    /**
     * 报文信息查询
     * @param msgLogId                      irm_msg_log的主键ID
     * @return                              应答结果
     * @throws GadgetsServiceException      服务异常
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/queryMsgContent.do")
    public HttpBaseResp<String> getMsgContant(Long msgLogId) throws GadgetsServiceException{
        MsgLogQueryDTO queryDTO = new MsgLogQueryDTO();
        queryDTO.setId(msgLogId);
        List<IrmMsgLog> msgLogList = draftOperService.selectMsgContent(queryDTO, null);
        HttpBaseResp<String> ret = new HttpBaseResp<>();
        if(null!=msgLogList && msgLogList.size()>0){
            ret.setErrorMsg(msgLogList.get(0).getDraftContent());
        }else {
            ret.setErrorMsg("未查询到报文信息");
        }
        return ret;
    }

}
